package com.tjsinfo.tjsoa.common;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

public class TestRiQi {
	
	  public static String getMonday(Date date){
		   Calendar c = Calendar.getInstance();
		   c.setTime(date);
		   c.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
		   return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
	}
	  public static String getTuesday(Date date){
		   Calendar c = Calendar.getInstance();
		   c.setTime(date);
		   c.set(Calendar.DAY_OF_WEEK,Calendar.TUESDAY);
		   return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
	}
	  public static String getWEDNESDAY(Date date){
		   Calendar c = Calendar.getInstance();
		   c.setTime(date);
		   c.set(Calendar.DAY_OF_WEEK,Calendar.WEDNESDAY);
		   return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
	}
	  public static String getTHURSDAY(Date date){
		   Calendar c = Calendar.getInstance();
		   c.setTime(date);
		   c.set(Calendar.DAY_OF_WEEK,Calendar.THURSDAY);
		   return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
	}
	  public static String getFRIDAY(Date date){
		   Calendar c = Calendar.getInstance();
		   c.setTime(date);
		   c.set(Calendar.DAY_OF_WEEK,Calendar.FRIDAY);
		   return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
	}
	  public static String getSATURDAY(Date date){
		   Calendar c = Calendar.getInstance();
		   c.setTime(date);
		   c.set(Calendar.DAY_OF_WEEK,Calendar.SATURDAY);
		   return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
	}
	  public static String getSUNDAY(Date date){
		   Calendar c = Calendar.getInstance();
		   c.setTime(date);
		   c.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
		   return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
	}
	//��ǰ����ǰ������ߺ��������

	  public static Date afterNDay(Date date,int n){
	         Calendar c=Calendar.getInstance();
	         DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
	         c.setTime(date);
	         c.add(Calendar.DATE,n);
	         Date d2=c.getTime();
	         //String s=df.format(d2);
	         return d2;
	    }
	  public static Date getMondayDate(Date date){
		   Calendar c = Calendar.getInstance();
		   c.setTime(date);
		   c.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
		   return c.getTime();
	}
	  public static Date getTuesdayDate(Date date){
		   Calendar c = Calendar.getInstance();
		   c.setTime(date);
		   c.set(Calendar.DAY_OF_WEEK,Calendar.TUESDAY);
		   return c.getTime();
	}
	  public static Date getWEDNESDAYDate(Date date){
		   Calendar c = Calendar.getInstance();
		   c.setTime(date);
		   c.set(Calendar.DAY_OF_WEEK,Calendar.WEDNESDAY);
		   return c.getTime();
	}
	  public static Date getTHURSDAYDate(Date date){
		   Calendar c = Calendar.getInstance();
		   c.setTime(date);
		   c.set(Calendar.DAY_OF_WEEK,Calendar.THURSDAY);
		   return c.getTime();
	}
	  public static Date getFRIDAYDate(Date date){
		   Calendar c = Calendar.getInstance();
		   c.setTime(date);
		   c.set(Calendar.DAY_OF_WEEK,Calendar.FRIDAY);
		   return c.getTime();
	}
	  public static Date getSATURDAYDate(Date date){
		   Calendar c = Calendar.getInstance();
		   c.setTime(date);
		   c.set(Calendar.DAY_OF_WEEK,Calendar.SATURDAY);
		   return c.getTime();
	}
	  public static Date getSUNDAYDate(Date date){
		   Calendar c = Calendar.getInstance();
		   c.setTime(date);
		   c.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
		   return c.getTime();
	}
	  public static Date getMonthFirstDay(Date date)
	  {
		  int   year,month,day;   
          year   =   date.getYear();   
          month   =   date.getMonth();   
          day   =   1;   
          Date   d   =   new   Date(year,month,1);   
          return   d;   

	  }
	  
	  public static Date getMonthLastDay(Date date)
	  {
		  int   year,month,day;   
          year   =   date.getYear();   
          month   =   date.getMonth()+1;   
          day   =1;   
          if(month   >11){   
              year++;   
              month=0;   
          }   
          Date   d   =     new   Date(year,month,1);   
          d.setDate(d.getDate()-1);   
          return   d;   

	  }
	  public static String getQuarter(Date date) {
		  String quarter = null;
		  Calendar calendar =  Calendar.getInstance();
		  calendar.setTime(date);
		  switch (calendar.get(Calendar.MONDAY)) {
		  case 0:
		   quarter = "第一季度";
		   break;
		  case 1:
		   quarter = "第一季度";
		   break;
		  case 2:
		   quarter = "第一季度";
		   break;
		  case 3:
		   quarter = "第二季度";
		   break;
		  case 4:
		   quarter = "第二季度";
		   break;
		  case 5:
		   quarter = "第二季度";
		   break;
		  case 6:
		   quarter = "第三季度";
		   break;
		  case 7:
		   quarter = "第三季度";
		   break;
		  case 8:
		   quarter = "第三季度";
		   break;
		  case 9:
		   quarter = "第四季度";
		   break;
		  case 10:
		   quarter = "第四季度";
		   break;
		  case 11:
		   quarter = "第四季度";
		   break;
		  }
		  return quarter;
		 }
	  
	  public static  String jisuanBanNian(Date date)
	  {
		  
		  Calendar calendar =  Calendar.getInstance();
		  calendar.setTime(date);
		  if (calendar.get(Calendar.MONDAY) < 5) {
			return "上半年";
		} else {
			return "下半年";
		}
	  }
	  /**
		 * 计算一个月的第一天
		 * @param date
		 */
		public static int monthfirst(Date date) {
			Calendar c = Calendar.getInstance();

			c.set(c.YEAR, date.getYear());
			c.set(c.MONTH, date.getMonth());
			return c.getActualMinimum(c.DAY_OF_MONTH);
			//System.out.println(c.getActualMaximum(c.DAY_OF_MONTH));   

		}

		/**
		 * 计算一个月的最后一天
		 * @param args
		 */
		public static int monthlast(Date date) {
			Calendar c = Calendar.getInstance();

			c.set(c.YEAR, date.getYear());
			c.set(c.MONTH, date.getMonth());
			c.set(c.DAY_OF_MONTH,1);
			return c.getActualMaximum(c.DAY_OF_MONTH);

		}
		//实现给定某日期，判断是星期几。
		public static String jishuxingqi(String str)
		{
			 // ----------------------
			  //实现给定某日期，判断是星期几。
			  //------------------------
			  SimpleDateFormat formatYMD = new SimpleDateFormat("yyyy-MM-dd");
			  //formatYMD表示的是yyyy-MM-dd格式
			  SimpleDateFormat formatD = new SimpleDateFormat("E");
			  //"E"表示"day in week"
			  Date d = null;
			  try
			  {
			   d = formatYMD.parse(str);
			   //将String 转换为符合格式的日期
			  }
			  catch(Exception e)
			  {
			   e.printStackTrace();
			  }
			  return  formatD.format(d);
		}
		/**
		 * 计算上一个月
		 * @param args
		 */
		public static Date upMonth(Date date) {
			Calendar c = Calendar.getInstance();
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			c.setTime(date);
			c.add(Calendar.MONTH, -1);
			Date d2 = c.getTime();
			//String s=df.format(d2);
			return d2;
		}

		/**
		 * 计算下一个月
		 * @param args
		 */
		public static Date downMonth(Date date) {
			Calendar c = Calendar.getInstance();
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			c.setTime(date);
			c.add(Calendar.MONTH, 1);
			Date d2 = c.getTime();
			//String s=df.format(d2);
			return d2;
		}
		
		/**
		 * 计算下一个年
		 * @param args
		 */
		public static Date downYear(Date date) {
			Calendar c = Calendar.getInstance();
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			c.setTime(date);
			c.add(Calendar.YEAR, 1);
			Date d2 = c.getTime();
			//String s=df.format(d2);
			return d2;
		}
		/**
		 * 计算上一个年
		 * @param args
		 */
		public static Date upYear(Date date) {
			Calendar c = Calendar.getInstance();
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			c.setTime(date);
			c.add(Calendar.YEAR, -1);
			Date d2 = c.getTime();
			//String s=df.format(d2);
			return d2;
		}

		/**
		 * 返回一个月的第一天
		 * @param date
		 * @return
		 */
		public static String firstMonth(Date date)
		{
			date.setDate(1);	
			return  DateTimeFromConver.zhuanhuariqi(date); 
		}
		/**
		 * 返回一个月的最后一天
		 * @param date
		 * @return
		 */
		public static String lastMonth(Date date)
		{
			date.setDate(monthlast(date));
			return  DateTimeFromConver.zhuanhuariqi(date); 
		}
		/**
		 * 返回当前时间是第几个星期
		 * @param date
		 * @return
		 */
		public static Integer selectWeekYear(Date date)
		{
			GregorianCalendar calendar = new GregorianCalendar(Locale.CHINA);
			calendar.setTime(date);
			return  calendar.get(GregorianCalendar.WEEK_OF_YEAR);
		}
		/**
		 * 给定第几个星期得出当前星期的第一天
		 * @param date
		 * @return
		 */
		public static String firstWeek(Integer id)
		{
			GregorianCalendar calendar = new GregorianCalendar(Locale.CHINA);
			calendar.set(GregorianCalendar.WEEK_OF_YEAR, id);
			calendar.set(GregorianCalendar.DAY_OF_WEEK,GregorianCalendar.MONDAY);
			return  DateTimeFromConver.zhuanhuariqi(calendar.getTime()); 
		}
		/**
		 * 给定第几个星期得出当前星期的最后一天
		 * @param date
		 * @return
		 */
		public static String lastWeek(Integer id)
		{
			GregorianCalendar calendar = new GregorianCalendar(Locale.CHINA);
			calendar.set(GregorianCalendar.WEEK_OF_YEAR, id +1);
			calendar.set(GregorianCalendar.DAY_OF_WEEK,GregorianCalendar.SUNDAY);
			return  DateTimeFromConver.zhuanhuariqi(calendar.getTime()); 
		}
		
		/**
		 * 计算下一天
		 * @param args
		 */
		public static Date downDate(Date date) {
			Calendar c = Calendar.getInstance();
			c.setTime(date);
			c.add(Calendar.DATE, 1);
			Date d2 = c.getTime();
			return d2;
		}
		
		/**
		 * 计算下几天
		 * @param args
		 */
		public static Date downDate(Date date,Integer count) {
			Calendar c = Calendar.getInstance();
			c.setTime(date);
			c.add(Calendar.DATE, count);
			Date d2 = c.getTime();
			return d2;
		}
}
